<template>
	<view class="work_report_container">
		<view v-if="feedBack && feedBack.status == 'APPROVING'" class="pending-review-view">
			<image class="pending-review-img" src="@/static/aidex/product/icon_pending_review.png"></image>
			<!-- <text class="pending-review-txt">BGD20230212100001</text> -->
			<text class="pending-review-tips">报工已提交，等待相关人员审核</text>
		</view>
		<uni-card spacing="0" padding="10">
			<view class="info-view">
				<text class="info-lable">生产工单号：</text>
				<text class="info-value">{{ feedBack ? feedBack.workorderCode : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">任务编号：</text>
				<text class="info-value">{{ feedBack ? feedBack.taskCode : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">产品编码：</text>
				<text class="info-value">{{ feedBack ? feedBack.itemCode : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">产品名称：</text>
				<text class="info-value">{{ feedBack ? feedBack.itemName : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">单位：</text>
				<text class="info-value">{{ feedBack ? feedBack.unitOfMeasure : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">报工时间：</text>
				<text class="info-value">{{ feedBack && feedBack.createTime ? feedBack.createTime : formatTime() }}</text>
				<!-- : -->
			</view>
		</uni-card>
		<uni-card spacing="0" padding="10">
			<view class="info-view">
				<text class="info-lable">工序：</text>
				<text class="info-value">{{ feedBack ? feedBack.processCode : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">工作站：</text>
				<text class="info-value">{{ feedBack ? feedBack.workstationName : '' }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">报工人员：</text>
				<text class="info-value">{{ feedBack ? feedBack.userName : '' }}</text>
			</view>
		</uni-card>
		<uni-card spacing="0" padding="10">
			<view class="info-view">
				<text class="info-lable">报工总数量：</text>
				<text class="uni-input">{{ feedBack.quantityFeedback }}</text>
			</view>
			<view class="info-view">
				<text class="info-lable">合格品数量：</text>
				<input type="number" v-model="feedBack.quantityQualified" class="uni-input" placeholder="请输入合格品数量" />
			</view>
			<view class="info-view">
				<text class="info-lable">不良品数量：</text>
				<input type="number" v-model="feedBack.quantityUnquanlified" class="uni-input" placeholder="请输入不良品数量" />
			</view>
		</uni-card>
		<view v-if="feedBack && feedBack.status != 'APPROVING'" class="submit-btn" @click="submitEvent('APPROVING')">提交</view>
		<view v-if="feedBack && feedBack.status == 'APPROVING'" class="submit-btn" @click="doneEvent">完成</view>
	</view>
</template>

<script>
import moment from 'moment';
export default {
	data() {
		return {
			submitStatus: 0,
			mode: undefined,
			feedBack: {
				quantityUnquanlified: '',
				quantityQualified: ''
			}
		};
	},
	watch: {
		feedBack: {
			handler(newVal, oldVal) {
				let un = 0;
				let qu = 0;

				if (this.feedBack.quantityUnquanlified) {
					un = this.feedBack.quantityUnquanlified;
				}
				if (this.feedBack.quantityQualified) {
					qu = this.feedBack.quantityQualified;
				}
				this.feedBack.quantityFeedback = parseFloat(un) + parseFloat(qu);
				console.log(this.feedBack.quantityFeedback);
			},
			immediate: true,
			deep: true
		}
	},
	onLoad(options) {
		this.mode = options.mode || undefined;
		if (!this.mode) {
			uni.showToast({
				title: '参数错误'
			});
			return;
		}
		if (this.mode == 'add') {
			const task = this.vuex_currentTask;
			this.feedBack = Object.assign(this.feedBack, {
				feedbackType: 'SELF',
				workorderId: task.workorderId,
				workorderCode: task.workorderCode,
				workorderName: task.workorderName,
				taskId: task.taskId,
				taskCode: task.taskCode,
				unitOfMeasure: task.unitOfMeasure,
				itemName: task.itemName,
				itemId: task.itemId,
				itemCode: task.itemCode,
				userName: this.vuex_user.userName,
				userId: this.vuex_user.userId,
				nickName: this.vuex_user.nickName,
				workstationId: task.workstationId,
				workstationCode: task.workstationCode,
				workstationName: task.workstationName,
				quantityFeedback: '',
				quantityQualified: '',
				quantityUnquanlified: '',
				processId: task.processId,
				processName: task.processName,
				processCode: task.processCode,
				status: 'PREPARE'
			});
			console.log(this.feedBack);
		} else if (this.mode == 'updata') {
			console.log(this.vuex_currentFeedBack);
			this.feedBack = Object.assign(this.feedBack, this.vuex_currentFeedBack);
		}
	},
	onNavigationBarButtonTap(button) {
		if (button.id == 'saveEvent') {
			this.submitEvent('PREPARE');
		}
	},
	methods: {
		doneEvent() {
			uni.navigateBack({
				delta: 1
			});
		},

		submitEvent(statusParams) {
			if (!this.feedBack) {
				uni.showToast({
					title: '参数错误'
				});
				return;
			}
			// if(this.feedBack.quantityFeedback == ''){
			// 	uni.showToast({
			// 		title:"请输入报工总数量",
			// 		icon:"none"
			// 	})
			// 	return;
			// }
			if (this.feedBack.quantityQualified == '') {
				uni.showToast({
					title: '请输入报工合格品数量',
					icon: 'none'
				});
				return;
			}
			if (this.feedBack.quantityUnquanlified == '') {
				uni.showToast({
					title: '请输入报工不良品数量',
					icon: 'none'
				});
				return;
			}
			// const params = {
			// 	feedbackType:this.feedBack.feedbackType,
			// 	workorderId:this.feedBack.workorderId,
			// 	workstationId:this.feedBack.workstationId,
			// 	taskId:this.feedBack.taskId,
			// 	itemId:this.feedBack.itemId,
			// 	itemCode:this.feedBack.itemCode,
			// 	itemName:this.feedBack.itemName,
			// 	unitOfMeasure:this.feedBack.unitOfMeasure,
			// 	processId:this.feedBack,
			// 	processId:this.feedBack.processId,
			// 	processName:this.feedBack.processName,
			// 	processCode:this.feedBack.processCode,
			// 	userName:this.vuex_user.userName,
			// 	quantityFeedback:this.feedBack.quantityFeedback,
			// 	quantityQualified:this.feedBack.quantityQualified,
			// 	quantityUnquanlified:this.feedBack.quantityUnquanlified,
			// }
			uni.showLoading({
				title: '加载中...',
				icon: 'none'
			});
			this.feedBack = Object.assign(this.feedBack, { status: statusParams });
			if (this.feedBack.recordId) {
				this.$u.api.feedback
					.updataFeedBack(this.feedBack)
					.then(res => {
						uni.$emit('updataReportList');
						this.feedBack = Object.assign(this.feedBack, res.data || { status: statusParams });
						uni.hideLoading();
						this.submitStatus = 1;
					})
					.catch(err => {
						console.log(err);
						uni.hideLoading();
					});
			} else {
				this.$u.api.feedback
					.addFeedBack(this.feedBack)
					.then(res => {
						console.log(res.data);
						this.feedBack = Object.assign(this.feedBack, res.data || {});
						uni.$emit('updataReportList');
						this.submitStatus = 0;
						uni.hideLoading();
					})
					.catch(err => {
						console.log(err);
						uni.hideLoading();
					});
			}
		},
		formatTime() {
			return moment().format('yyyy-MM-DD hh:mm:ss');
		}
	}
};
</script>

<style lang="scss">
.work_report_container {
	padding-bottom: 50rpx;
	.submit-btn {
		background-color: #1296db;
		color: #fff;
		font-size: 28rpx;
		line-height: 80rpx;
		margin: 0 50rpx;
		text-align: center;
		border-radius: 100rpx;
	}
	.pending-review-view {
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.pending-review-img {
			width: 100rpx;
			height: 100rpx;
		}
		.pending-review-txt {
			color: #333333;
			font-size: 30rpx;
			line-height: 50rpx;
		}
		.pending-review-tips {
			color: #9a9a9a;
			font-size: 26rpx;
			margin-top: 10rpx;
		}
	}
	.info-view {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		line-height: 60rpx;
		.info-lable {
			font-size: 28rpx;
			color: #333333;
		}
		.info-value {
			font-size: 28rpx;
			color: #9a9a9a;
		}
		.uni-input {
			flex: 1;
			margin-left: 15rpx;
			font-size: 28rpx;
			color: #333333;
			// border-bottom: 1px solid #9a9a9a;
			text-align: right;
		}
	}
}
</style>
